package cn.seaboot.admin.security.web;


import cn.seaboot.admin.mvc.Pagination;
import cn.seaboot.admin.mvc.Result;
import cn.seaboot.admin.mvc.argument.Params;
import cn.seaboot.admin.security.bean.SecurityUrl;
import cn.seaboot.admin.security.service.SecurityUrlService;
import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Map;

/**
 * 接口权限配置 [t_sys_security_url] Controller
 *
 * @author Mr.css
 * @version 2021-06-16 11:57
 */
@Controller
@Tag(name = "接口权限配置 [t_sys_security_url] 管理")
@RequestMapping("sys/security/url")
public class SecurityUrlCtrl {

    @Resource
    private SecurityUrlService securityUrlService;

    /**
     * 接口权限配置 - 增
     *
     * @param pojo pojo
     * @return update row's count
     */
    @ResponseBody
    @Operation(description = "接口权限配置 [t_sys_security_url] 增")
    @RequestMapping(value = "info", method = RequestMethod.POST)
    public int saveInfo(@Validated SecurityUrl pojo) {
        return securityUrlService.insert(pojo);
    }

    /**
     * 接口权限配置 - 删
     *
     * @param id id
     * @return update row's count
     */
    @ResponseBody
    @Operation(description = "接口权限配置 [t_sys_security_url] 删")
    @RequestMapping(value = "info", method = RequestMethod.DELETE)
    public int deleteInfo(@Params String id) {
        return securityUrlService.deleteById(id);
    }

    /**
     * 接口权限配置 - 查
     *
     * @param id id
     * @return pojo
     */
    @ResponseBody
    @Operation(description = "接口权限配置 [t_sys_security_url] 查")
    @RequestMapping(value = "info", method = RequestMethod.GET)
    public SecurityUrl queryById(@Params String id) {
        return securityUrlService.queryById(id);
    }

    /**
     * 接口权限配置 - 改
     *
     * @param id   id
     * @param pojo pojo
     * @return update row's count
     */
    @ResponseBody
    @Operation(description = "接口权限配置 [t_sys_security_url] 改")
    @RequestMapping(value = "info", method = RequestMethod.PUT)
    public int updateInfo(@Params String id, @Validated SecurityUrl pojo) {
        return securityUrlService.updateById(pojo);
    }


    /**
     * 接口权限配置 - 初始化
     */
    @ResponseBody
    @Operation(description = "接口权限配置 [t_sys_security_url] 初始化")
    @RequestMapping(value = "rebuild", method = RequestMethod.PUT)
    public Result rebuild() {
        securityUrlService.rebuildAllApi();
        return Result.succeed();
    }

    /**
     * 接口权限配置 - 查列表
     *
     * @param params 参数
     * @return PageInfo
     */
    @Operation(description = "接口权限配置 [t_sys_security_url] 列表查询")
    @Parameters({
            @Parameter(name = "page", description = "页号"),
            @Parameter(name = "limit", description = "行数")})
    @ResponseBody
    @RequestMapping(value = "page", method = RequestMethod.GET)
    public PageInfo<SecurityUrl> queryList(@RequestParam Map<String, Object> params) {
        return Pagination.apply(params, map-> securityUrlService.queryList(map));
    }
}
